//
//  ViewController.m
//  UseData
//
//  Created by Lenny on 16/5/15.
//  Copyright © 2016年 Lenny. All rights reserved.
//

#import "ViewController.h"
#import "User.h"
#import "UserModel.h"
#import "Other.h"

#import <objc/runtime.h>

#import "CoreDataManager.h"

#import "UIImageView+load.h"

@interface ViewController ()
{
    CoreDataManager *manager;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _myAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    unsigned int proCout = 0;
    objc_property_t *pros = class_copyPropertyList([self class], &proCout);
    for (unsigned int i = 0; i < proCout; i ++) {
        
        objc_property_t pro = pros[i];
        const char *name = property_getName(pro);
        const char *attri = property_getAttributes(pro);
        NSLog(@"%@",[self valueForKey:[NSString stringWithFormat:@"%s",name]]);
        NSLog(@"%s,%s",name,attri);
    }
    
    manager = [CoreDataManager share];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageV.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:imageV];
    [imageV loadUrl:@"http://upload.fnbird.com/shop/common/default_goods_image_1280.gif"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)delete:(id)sender {
    
    
    [manager deleteWithEnity:@"User" withSql:@"age==\"age\""];
    [manager saveData];
    
//    NSFetchRequest *requset = [[NSFetchRequest alloc] init];
//    NSEntityDescription *user = [NSEntityDescription entityForName:@"User" inManagedObjectContext:_myAppDelegate.managedObjectContext];
//    [requset setEntity:user];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age==%@",@"age"];
//    [requset setPredicate:predicate];
//    
//    NSError *error = nil;
//    NSMutableArray *arr = [[_myAppDelegate.managedObjectContext executeFetchRequest:requset error:&error] mutableCopy];
//    if (arr == nil) {
//        
//        NSLog(@"error:%@",error);
//    }
//    for (User *user in arr) {
//        [_myAppDelegate.managedObjectContext deleteObject:user];
//    }
//    if ([_myAppDelegate.managedObjectContext save:&error]) {
//        NSLog(@"error:%@,%@",error,[error userInfo]);
//    }
}

//插入数据
- (IBAction)add:(id)sender {
    
    
    Other *other = [manager setEnityWithString:@"Other"];
    other.pic = @"pic1";
    other.name = @"other";
    other.uid = @"1343535";
    [manager saveData];
    
    
    User *user = [manager setEnityWithString:@"User"];
    user.name = _nameText.text;
    user.age = _ageText.text;
    user.sex = _sexText.text;
    [manager saveData];
    
    
//    [manager addWithEnity:user];
    
//    User* user=(User *)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.myAppDelegate.managedObjectContext];
//    [user setName:_nameText.text];
//    [user setAge:_ageText.text];
//    [user setSex:_sexText.text];
//    NSError* error;
//    BOOL isSaveSuccess=[_myAppDelegate.managedObjectContext save:&error];
//    if (!isSaveSuccess) {
//        NSLog(@"Error:%@",error);
//    }else{
//        NSLog(@"Save successful!");
//    }
}

- (IBAction)update:(id)sender {
    
    
    //name==\"wangliang\" and age=\"23\" 两个条件
    NSMutableArray *arr = [manager checkWithEnity:@"User" withSql:@"name==\"wangliang\" and age=\"23\""];
//    for (User *user in arr) {
//        
//        user.sex = @"women";
//    }
//    [manager saveData];
    
    
    NSMutableArray *arr1 = [manager checkWithEnity:@"User" withSql:@"age < \"23\""];
    NSMutableArray *all = [manager checkWithEnity:@"User" withSql:@""];
//
//    NSFetchRequest* request=[[NSFetchRequest alloc] init];
//    NSEntityDescription* user=[NSEntityDescription entityForName:@"User" inManagedObjectContext:_myAppDelegate.managedObjectContext];
//    [request setEntity:user];
//    //查询条件
//    NSPredicate* predicate=[NSPredicate predicateWithFormat:@"name==%@",@"wangliang"];
//    [request setPredicate:predicate];
//    NSError* error=nil;
//    NSMutableArray* mutableFetchResult=[[_myAppDelegate.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
//    if (mutableFetchResult==nil) {
//        NSLog(@"Error:%@",error);
//    }
//    NSLog(@"The count of entry: %i",[mutableFetchResult count]);
//    //更新age后要进行保存，否则没更新
//    for (User* user in mutableFetchResult) {
//        [user setAge:@"2"];
//        
//    }
//    [_myAppDelegate.managedObjectContext save:&error];
//    
}
@end
